package com.sinosun.autotestmachine.tools.utils;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sinosun.autotestmachine.tools.ValueInfo;
import com.sinosun.autotestmachine.tools.ValueType;

public class Utils {
	
	public static String calendarToString(Calendar calendar) {	//当前时间转成对应的字符串格式
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		String monthStr = new String();
		if (month < 10) {
			monthStr = "0" + month;
		} else {
			monthStr = "" + month;
		}
		String dayStr = new String();
		if (day < 10) {
			dayStr = "0" + day;
		} else {
			dayStr = "" + day;
		}
		String hourStr = new String();
		if (hour < 10) {
			hourStr = "0" + hour;
		} else {
			hourStr = "" + hour;
		}
		String minuteStr = new String();
		if (minute < 10) {
			minuteStr = "0" + minute;
		} else {
			minuteStr = "" + minute;
		}
		String secondStr = new String();
		if (second < 10) {
			secondStr =  "0" + minute;
		} else {
			secondStr =  "" + minute;
		}
		
		String currentTimeStr = year + "_" + monthStr + "_" + dayStr + "_" + hourStr + "_" + minuteStr + "_" + secondStr;
		return currentTimeStr;
	}
	
    public static long getWebsiteDatetime(String webUrl){
		try {
			URL url = new URL(webUrl);
			URLConnection uc = url.openConnection();
			uc.connect();
			return uc.getDate();// 读取网站日期时间
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return 0;
    }
    
    public static long getWebsiteDatetime() {
    	return getWebsiteDatetime("http://www.baidu.com") / 1000 + 5444777L;
    }
    
    public static ValueInfo getUserRealInfo(String res) {
    	ValueInfo ret = new ValueInfo();
    	
		Pattern pattern = Pattern.compile("\\#[a-zA-Z]*[0-9]*\\#");
		Matcher matcher = pattern.matcher(res);
		if(matcher.matches()) {
			ret.setValueType(ValueType.INDEX);
			ret.setValueContent(res.substring(1, res.length()-1));
		} else {
			ret.setValueType(ValueType.CONTENT);
			ret.setValueContent(res);
		}
    	return ret;
    }
    
    public static ValueInfo getRealVoteInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getSendRealInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getTopicRealInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getGroupMstpInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getMsgRealInfo(String res) {
    	return getGroupRealInfo(res);
    }
    
    public static ValueInfo getFileIDRealInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getNodeIDRealInfo(String res) {
    	return getUserRealInfo(res);
    }
    
    public static ValueInfo getGroupRealInfo(String res) {
    	ValueInfo ret = new ValueInfo();
    	
		Pattern pattern = Pattern.compile("\\#[0-9]+\\#");
		Matcher matcher = pattern.matcher(res);
		if(matcher.matches()) {
			ret.setValueType(ValueType.INDEX);
			ret.setValueContent(res.substring(1, res.length()-1));
		} else {
			ret.setValueType(ValueType.CONTENT);
			ret.setValueContent(res);
		}
    	return ret;
    }
}
